/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.imoney.payment.member.handler;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.imoney.framework.base.entity.IRequest;
import com.imoney.framework.base.entity.Response;
import com.imoney.framework.base.exception.ServiceException;
import com.imoney.payment.chain.HandlerChain;
import com.imoney.payment.common.BaseCode;
import com.imoney.payment.member.domain.Addr;
import com.imoney.payment.member.domain.Member;
import com.imoney.payment.member.domain.MemberInfo;
import com.imoney.payment.member.domain.RegCompleteRequest;
import com.imoney.payment.member.domain.RegRequest;
import com.imoney.payment.member.enums.AccStatus;
import com.imoney.payment.member.generator.AddrGenerator;
import com.imoney.payment.member.generator.MemberGenerator;
import com.imoney.payment.member.generator.MemberInfoGenerator;
import com.imoney.payment.member.service.IAddrService;
import com.imoney.payment.member.service.IMemberInfoService;
import com.imoney.payment.member.service.IMemberService;

/**
 * 类说明：交易业务处理<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * 
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	zengxx 曾宪新	|		2013-6-6		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author zengxx 曾宪新(Xavier.zeng)
 * 
 *         CreateDate: 2013-6-6
 */
@Service
public class MemberHandler extends AbstractHandler<Member> {

	@Resource
	private IMemberService memberService;

	@Resource
	private IMemberInfoService memberInfoService;

	@Resource
	private IAddrService addrService;

	@Resource
	private MemberGenerator memberGenerator;

	@Resource
	private MemberInfoGenerator memberInfoGenerator;

	@Resource
	private AddrGenerator addrGenerator;

	@Override
	public void doHandler(HandlerChain<Member> chain, IRequest request,
			Response<Member> response) throws ServiceException {
		Member member = null;

		/** 验证账户名 - 注册 */
		if (request instanceof RegRequest) {
			RegRequest regRequest = (RegRequest) request;
			member = memberGenerator.generate(regRequest);

			memberService.add(member);
		}
		/** 填写账户信息 - 注册 */
		else if (request instanceof RegCompleteRequest) {
			RegCompleteRequest regRequest = (RegCompleteRequest) request;
			/**
			 * 补充完成会员信息
			 */
			member = memberGenerator.generate(regRequest);
			member.setId(response.getData().getId());
			memberService.update(member);

			/**
			 * 创建物流信息
			 */
			MemberInfo memberInfo = memberInfoGenerator.generate(regRequest);
			memberInfo.setMemberNo(response.getData().getMemberNo());
			memberInfoService.add(memberInfo);

			/**
			 * 生成常用地址
			 */
			Addr addr = addrGenerator.generate(regRequest);
			addr.setMemberNo(memberInfo.getMemberNo());
			addrService.addAddr(addr);
		}
		response.setData(member);

		/** 处理下一个任务 */
		chain.doHandler(chain, request, response);

		/**
		 * 目前为止，状态成功更新状态
		 */
		if (BaseCode.SUCCEED == response.getCode()) {
			if (AccStatus.activate.equals(member.getStatus())) {
				member.setStatus(AccStatus.login);
				memberService.update(member);
				response.getData().setNickname(member.getNickname());
			}
		}
	}

}
